package controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.AppointmentRule;
import service.AppointmentRuleService;
import java.util.List;

@Api(tags = "预约规则管理", description = "预约规则相关的CRUD操作")
@RestController
@RequestMapping("/appointment-rules")
public class AppointmentRuleController {

    @Autowired
    private AppointmentRuleService appointmentRuleService;

    @PostMapping("/add")
    @ApiOperation(value = "新增预约规则", notes = "添加一个新的预约规则")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int addRule(@ApiParam(value = "预约规则实体", required = true) @RequestBody AppointmentRule rule) {
        return appointmentRuleService.addRule(rule);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新预约规则", notes = "更新预约规则的基本信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 404, message = "规则不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int updateRule(@ApiParam(value = "预约规则实体(包含ruleId)", required = true) @RequestBody AppointmentRule rule) {
        return appointmentRuleService.updateRule(rule);
    }

    @DeleteMapping("/{ruleId}")
    @ApiOperation(value = "删除预约规则", notes = "根据规则ID删除预约规则")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Integer.class),
            @ApiResponse(code = 404, message = "规则不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public int deleteRule(@ApiParam(value = "规则ID", required = true) @PathVariable("ruleId") Integer ruleId) {
        return appointmentRuleService.deleteRule(ruleId);
    }

    @GetMapping("/{ruleId}")
    @ApiOperation(value = "获取预约规则详情", notes = "根据规则ID获取预约规则信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = AppointmentRule.class),
            @ApiResponse(code = 404, message = "规则不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public AppointmentRule getRule(@ApiParam(value = "规则ID", required = true) @PathVariable("ruleId") Integer ruleId) {
        return appointmentRuleService.getRuleById(ruleId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取预约规则列表", notes = "获取所有预约规则的基本信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = AppointmentRule.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<AppointmentRule> getAllRules() {
        return appointmentRuleService.getAllRules();
    }

    @GetMapping("/search")
    @ApiOperation(value = "根据规则名称搜索", notes = "根据规则名称模糊搜索预约规则")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = AppointmentRule.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<AppointmentRule> searchRulesByName(@ApiParam(value = "规则名称", required = true) @RequestParam("ruleName") String ruleName) {
        return appointmentRuleService.getRulesByRuleName(ruleName);
    }
}
